package com.作业.第十章作业.Homework06;

/**
 * 作    者 ：吾路巴尼
 * 创建时间 ：2025/2/13-23:18
 */
public class main {
    public static void main(String[] args) {
        Person p1 = new Person(VehiclesFactory.getHorse(), "张三");
        p1.common();
        p1.passRiver();
        p1.passRiver();
        p1.passRiver();
        p1.common();
        p1.fly();



    }
}
interface Vehicles{  //交通工具
    void work();
}
class VehiclesFactory{
    private static Horse horse = new Horse();//交通工具
   public static Horse getHorse(){
       return horse;
   }
   public static Boat getBoat(){
       return new Boat();
   }
   public static Plane getPlane(){
       return new Plane();
   }

}
class Horse implements Vehicles{  //马
    @Override
    public void work() {
        System.out.println("🐎跑起来");
    }

    public Horse() {
        System.out.println("马匹");
    }
}
class Boat implements Vehicles{  //船
    @Override
    public void work() {
        System.out.println("船划水");
    }
}

class Plane implements Vehicles{

    @Override
    public void work() {
        System.out.println("飞机飞起来");
    }
}
class Person{  //人
    private Vehicles vehicles;
    private String name;

    public Person(Vehicles vehicles, String name) {
       this.vehicles= vehicles;
        this.name = name;
    }
  public void passRiver(){
        if(!(vehicles instanceof Boat)) {
            vehicles = VehiclesFactory.getBoat();
            vehicles .work();
            System.out.println("创建了船");
        } else vehicles.work();
  }
  public void common(){
        if(!(vehicles instanceof Horse)) {
            vehicles = VehiclesFactory.getHorse();
            vehicles .work();
        } else vehicles.work();
  }

  public void fly(){
        if(!(vehicles instanceof Plane)){
            vehicles = VehiclesFactory.getPlane();
            vehicles.work();
            System.out.println("创建了飞机");
        }
        else vehicles.work();
  }

}